package com.example.dell.ZYFYPT_226wzy.model;

import com.example.dell.ZYFYPT_226wzy.bean.CaseBean;
import com.example.dell.ZYFYPT_226wzy.iface.CaseListener;
import com.example.dell.ZYFYPT_226wzy.iface.Caseiface;
import com.example.dell.ZYFYPT_226wzy.service.CaseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class CaseModel implements Caseiface {

    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";

    //构造方法
    public CaseModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final CaseListener listener) {
        CaseService caseService = retrofit.create(CaseService.class);
        Call<List<CaseBean>> call = caseService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<CaseBean>>() {
            @Override
            public void onResponse(Call<List<CaseBean>> call, Response<List<CaseBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("on response fail");
                }
            }

            @Override
            public void onFailure(Call<List<CaseBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
